package com.longrise.android.loaddata.newloaddata;

import android.app.Activity;
import android.content.Context;
import android.os.Environment;
import android.os.Handler;

import com.longrise.LEAP.Base.IO.JSONSerializer;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.util.PreferenceUtils;

import java.io.File;
import java.util.HashMap;

/**
 * Created by chengsan on 16/5/20.
 */
public abstract class LoadDataManagerFather
{

    public enum ResultType
    {
        NoNetWorks,   //没有网络
        LoginOut,     //用户被挤掉
        Success,      //成功
        Failure       //失败
    }
    //上下文对象
    public Context context;
    //cookID
    public String cookID;
    //登录数据集合
    public EntityBean loginInfoBean;
    //版本
    public String appVersion;
    //请求方式 1:get 2:POST 默认POST请求
    public int requestType = 2;
    //没有网络时，内部是否提示，默认不提示
    public boolean showNetNotice = false;
    //请求类型Key
    public static final String REQUEST_TYPE_KEY_NAME = "requestTypeKeyName";
    //SP名称
    public static final String SP_NAME = "LoadDataManagerFatherName";
    //存储登录数据的Key名
    public static final String LOGININFO_KEY_NAME = "loginInfoBeanKeyName";
    //IP名称
    public static final String IP_KEY_NAME = "ipKeyName";
    //服务名称
    public static final String SERVICENAME_KEY_NAME = "serviceNameKeyName";
    //参数名称
    public static final String PARAMBEAN_KEY_NAME = "paramBeanKeyName";
    //存储版本的Key名
    public static final String APPVERSION_KEY_NAME = "appVersionKeyName";
    //缓存目录的Key
    public static final String CACHE_DIC_KEY = "cacheDicKeyName";
    //手机设置的时间的Key
    public static final String CONFIG_CACHE_MOBILE_TIMEOUT_KEY_NAME = "mobileTimeOutKeyName";
    //WI-FI设置的时间间隔Key
    public static final String CONFIG_CACHE_WIFI_TIMEOUT_KEY_NAME = "wifiTimeOutKeyName";
    //是否显示提示的Key
    public static final String SHOW_NOTICE_KEY_NAME = "showNetNoticeKeyName";
    //连接超时Key
    public static final String CONNECT_TIME_OUT_NAME = "conTimeOut";
    //读取数据超时Key
    public static final String SO_TIME_OUT_NAME = "soTimeOut";
    //哈西数组，用于存放请求的对象
    public HashMap<String,HttpRunnableFather> httpRunnableHashMap;
    //请求次数
    public int count = 0;
    //缓存目录
    public String cacheDic = null;
    //请求的时间间隔
    public int mobileTimeOut  = 30000;  //手机网络默认5分钟
    public int wifiTimeOut    = 18000;   //WI-FI网络默认3分钟

    //用户Session过期标志符数组
    public static final String SESSION_TIMEOUT_KEY_NAME = "sessionTimeOutKeyName";
    //用户被挤掉标志符数组
    public static final String LOGINOUT_KEY_NAME = "loginOutKeyName";

    //用户Session过期标志符数组
    public String[] sessionTimeOutArray;
    //用户被挤掉标志符数组
    public String[] loginOutArray;

    //请求连接超时时间,单位：毫秒
    public int connectTimeOut = -1;
    //读取数据超时时间
    public int soTimeOut = -1;

    public Handler handler = null;


    public LoadDataManagerFather()
    {
        httpRunnableHashMap = new HashMap<String, HttpRunnableFather>();
    }

    public LoadDataManagerFather(Context context)
    {
        this.context = context.getApplicationContext();
        handler = new Handler();
        httpRunnableHashMap = new HashMap<String, HttpRunnableFather>();

        if(context != null)
        {
            //从SP中获取登录信息
            String loginInfoStr = PreferenceUtils.getPrefString(SP_NAME,Activity.MODE_PRIVATE,context,LOGININFO_KEY_NAME,null);
            if(null != loginInfoStr)
            {
                this.loginInfoBean = (EntityBean) JSONSerializer.getInstance().DeSerialize(loginInfoStr,EntityBean.class);
            }

            //从SP中获取版本信息
            this.appVersion = PreferenceUtils.getPrefString(SP_NAME,Activity.MODE_PRIVATE,context,APPVERSION_KEY_NAME,null);

            //从SP中获取请求方式信息
            this.requestType = PreferenceUtils.getPrefInt(SP_NAME,Activity.MODE_PRIVATE,context,REQUEST_TYPE_KEY_NAME,2);

            this.showNetNotice = PreferenceUtils.getPrefBoolean(SP_NAME,Activity.MODE_PRIVATE,context,SHOW_NOTICE_KEY_NAME,false);

            this.cacheDic = PreferenceUtils.getPrefString(SP_NAME,Activity.MODE_PRIVATE,context,CACHE_DIC_KEY,
                    Environment.getExternalStorageDirectory().getPath() + "/" + context.getPackageName());

            this.mobileTimeOut = PreferenceUtils.getPrefInt(SP_NAME,Activity.MODE_PRIVATE,context,CONFIG_CACHE_MOBILE_TIMEOUT_KEY_NAME,30000);

            this.wifiTimeOut = PreferenceUtils.getPrefInt(SP_NAME,Activity.MODE_PRIVATE,context,CONFIG_CACHE_WIFI_TIMEOUT_KEY_NAME,18000);

            this.connectTimeOut = PreferenceUtils.getPrefInt(SP_NAME,Activity.MODE_PRIVATE,context,CONNECT_TIME_OUT_NAME,-1);

            this.soTimeOut = PreferenceUtils.getPrefInt(SP_NAME,Activity.MODE_PRIVATE,context,SO_TIME_OUT_NAME,-1);

            //获取登出标志数组
            String loginOutStr = PreferenceUtils.getPrefString(SP_NAME,Activity.MODE_PRIVATE,context,LOGINOUT_KEY_NAME,null);
            if(null != loginOutStr)
            {
                this.loginOutArray = loginOutStr.split(",");
            }

            //获取超时标志数组
            String sessionTimeOutStr = PreferenceUtils.getPrefString(SP_NAME,Activity.MODE_PRIVATE,context,SESSION_TIMEOUT_KEY_NAME,null);
            if(null != sessionTimeOutStr)
            {
                this.sessionTimeOutArray = sessionTimeOutStr.split(",");
            }
        }
    }


    /**
     * 请求服务，改服务可以上传文件
     * @param key
     *        用于取消请求使用，请用该类中的getKey()方法来生成
     * @param serviceUrl
     *        服务的URL
     * @param serviceName
     *        服务名
     * @param bean
     *        参数
     * @param openCache
     *        开启缓存
     * @param fileKey
     *        文件对象对应的Key
     * @param file
     *        需要上传的File文件对象
     * @param mimeType
     *        文件类型，如："image/pjpeg"
     * @param onRequestCompleteListener
     *        请求完成回调接口
     */
    public void callService3(String key,String serviceUrl,String serviceName, EntityBean bean,
                             boolean openCache,String fileKey,File file,String mimeType,
                             OnuploadFileLishtener onuploadFileLishtener,
                             OnRequestCompleteListener onRequestCompleteListener)
    {

    }

    //==========================GET()/SET()方法=============================


    public void setLoginOutArray(String[] loginOutArray)
    {
        this.loginOutArray = loginOutArray;
        StringBuffer tempStr = new StringBuffer();
        if(null != loginOutArray && loginOutArray.length > 0)
        {
            for (int i = 0;i< loginOutArray.length;i++)
            {
                tempStr.append(loginOutArray[i]);
                if(i != 0 && i != (loginOutArray.length-1))
                {
                    tempStr.append(",");
                }
            }
        }
        if(null != context)
        {
            PreferenceUtils.setPrefString(SP_NAME,
                    Activity.MODE_PRIVATE,context,LOGINOUT_KEY_NAME,tempStr.toString());
        }
    }

    public void setSessionTimeOutArray(String[] sessionTimeOutArray)
    {
        this.sessionTimeOutArray = sessionTimeOutArray;
        StringBuffer tempStr = new StringBuffer();
        if(null != sessionTimeOutArray && sessionTimeOutArray.length > 0)
        {
            for (int i = 0;i< sessionTimeOutArray.length;i++)
            {
                tempStr.append(sessionTimeOutArray[i]);
                if(i != 0 && i != (sessionTimeOutArray.length-1))
                {
                    tempStr.append(",");
                }
            }
        }
        if(null != context)
        {
            PreferenceUtils.setPrefString(SP_NAME,
                    Activity.MODE_PRIVATE,context,SESSION_TIMEOUT_KEY_NAME,tempStr.toString());
        }
    }

    public void setAppVersion(String appVersion)
    {
        this.appVersion = appVersion;
        if(null != context)
        {
            PreferenceUtils.setPrefString(SP_NAME,
                    Activity.MODE_PRIVATE,context,APPVERSION_KEY_NAME,appVersion);
        }
    }
    public String getAppVersion()
    {
        return appVersion;
    }

    public void setLoginInfoBean(EntityBean loginInfoBean)
    {
        this.loginInfoBean = loginInfoBean;
        if(null != context)
        {
            String loginInfoStr = JSONSerializer.getInstance().Serialize(loginInfoBean);
            PreferenceUtils.setPrefString(SP_NAME,Activity.MODE_PRIVATE,context,LOGININFO_KEY_NAME,loginInfoStr);
        }
    }
    public EntityBean getLoginInfoBean()
    {
        return loginInfoBean;
    }

    public void setRequestType(int requestType)
    {
        this.requestType = requestType;
        if(context != null)
        {
            PreferenceUtils.setPrefInt(SP_NAME,Activity.MODE_PRIVATE,context,REQUEST_TYPE_KEY_NAME,requestType);
        }
    }

    public int getRequestType()
    {
        return requestType;
    }

    public void setShowNetNotice(boolean showNetNotice)
    {
        this.showNetNotice = showNetNotice;
        if(context != null)
        {
            PreferenceUtils.setPrefBoolean(SP_NAME,Activity.MODE_PRIVATE,context,SHOW_NOTICE_KEY_NAME,showNetNotice);
        }
    }

    public void setCacheDic(String cacheDic)
    {
        this.cacheDic = cacheDic;
        if(context != null)
        {
            PreferenceUtils.setPrefString(SP_NAME,Activity.MODE_PRIVATE,context,CACHE_DIC_KEY,cacheDic);
        }
    }

    public String getCacheDic()
    {
        return cacheDic;
    }

    public void setMobileTimeOut(int mobileTimeOut)
    {
        this.mobileTimeOut = mobileTimeOut;
        if(context != null)
        {
            PreferenceUtils.setPrefInt(SP_NAME,Activity.MODE_PRIVATE,context,CONFIG_CACHE_MOBILE_TIMEOUT_KEY_NAME,mobileTimeOut);
        }
    }

    public int getMobileTimeOut()
    {
        return mobileTimeOut;
    }

    public void setWifiTimeOut(int wifiTimeOut)
    {
        this.wifiTimeOut = wifiTimeOut;
        if(context != null)
        {
            PreferenceUtils.setPrefInt(SP_NAME,Activity.MODE_PRIVATE,context,CONFIG_CACHE_WIFI_TIMEOUT_KEY_NAME,wifiTimeOut);
        }
    }

    public int getWifiTimeOut()
    {
        return wifiTimeOut;
    }

    /**
     * 设置连接超时时间
     * @param connectTimeOut
     *                      超时时间，单位毫秒
     */
    public void setConnectTimeOut(int connectTimeOut)
    {
        this.connectTimeOut = connectTimeOut;
        if(context != null)
        {
            PreferenceUtils.setPrefInt(SP_NAME,Activity.MODE_PRIVATE,context,CONNECT_TIME_OUT_NAME,connectTimeOut);
        }
    }

    /**
     * 设置读取超时时间
     * @param soTimeOut
     */
    public void setSoTimeOut(int soTimeOut)
    {
        this.soTimeOut = soTimeOut;
        if(context != null)
        {
            PreferenceUtils.setPrefInt(SP_NAME,Activity.MODE_PRIVATE,context,SO_TIME_OUT_NAME,soTimeOut);
        }
    }

    /**
     * 请求完成接口
     */
    public interface OnRequestCompleteListener
    {
        /**
         * 成功接口
         * @param key
         * @param service
         * @param result
         */
        void onSuccess(String key, String service, Object result);

        /**
         * 出错接口
         * @param key
         * @param service
         * @param resultType
         */
        void onError(String key, String service,ResultType resultType);

        /**
         * 完成接口，不论是成功或者失败都会调用
         * @param key
         * @param service
         */
        void onFinished(String key, String service);
    }

    /**
     * 上传文件进度回调接口
     */
    public interface OnuploadFileLishtener
    {
        /**
         * 回调方法
         * @param cuttrnt
         *          当前进度
         * @param total
         *          总进度
         */
        void onUpload(long cuttrnt,long total);
    }
}